package ch.usi.inf.pf2.node;

/* 
* @author Matthias.Hauswirth@unisi.ch

*/
import ch.usi.inf.pf2.cell.NumericValue;
import ch.usi.inf.pf2.cell.Value;

public abstract class ArithmeticOperation implements Node {

	private final Node left;
	private final Node right;
	
	
	public ArithmeticOperation(final Node left, final Node right) {
		this.left = left;
		this.right = right;
	}
	
	public final NumericValue evaluate() {
		final Value leftValue = left.evaluate();
		final Value rightValue = right.evaluate();
		if (leftValue instanceof NumericValue && rightValue instanceof NumericValue) {
			final NumericValue leftNumericValue = (NumericValue) leftValue;
			final double leftNumber = leftNumericValue.get();
			final NumericValue rightNumericValue = (NumericValue)rightValue;
			final double rightNumber = rightNumericValue.get();
			return new NumericValue(compute(leftNumber, rightNumber));
		} else {
			return null;
		}	
	}
	
	protected abstract double compute(double left, double right);

}
